// pages/consultant/consultant.js
const cf = require("../../config.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    consultantInfoList:[],
    eid:'',
    searchData:'',
    pageType:false,
    pageSize:10,
    pageNo:1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  changeSearchData(e){
    this.setData({
      searchData:e.detail.value
    })
  },
  clearSearchData(){
    console.log('***************')
    this.setData({
      searchData:''
    })
  },
  getconsultantInfoList(data,type=null){
    let that = this
    wx.showLoading({title:'正在加载...'})
    wx.request({
      //url:`http://10.73.158.91:8099/applet/mobile/visitcard/employee/queryByPhoneOrName`,
      url:`${cf.config.pageDomain}applet/mobile/visitcard/employee/queryByPhoneOrName`,
      header: {
        'content-type': 'application/json',
        'token': wx.getStorageSync('authName')
      },
      data,
      success:function(res){
        if(res.data&&res.data.length){
          res.data.map(item=>{
            if(item.labels){
              item.labels = JSON.parse(item.labels).map(child=>child.lab)
            }
          })
          if(type&&type=='search'){
            that.setData({consultantInfoList:res.data})
          } else {
            that.setData({consultantInfoList:[...that.data.consultantInfoList,...res.data]})
          }
        } else {
          that.setData({pageNo:that.data.pageNo-1});
        }
        wx.hideLoading()
      },
      fail:function(res){
        wx.hideLoading()
      }
    })
  },
  changeParams(){
    let that = this;
    let reg = /[\u4e00-\u9fa5]/ig,flag=false;
    this.data.searchData.split('').map(item=>{
      if(reg.test(item)){
        flag = true;
        return;
      }
    })
    let data = flag?{name:this.data.searchData}:{phone:this.data.searchData}
    data = {...data,pageNo:that.data.pageNo,pageSize:that.data.pageSize}
    return data
  },
  search(){
    this.setData({consultantInfoList:[]})
    let data = this.changeParams()
    data.pageNo = 1;
    this.getconsultantInfoList(data,'search')
  },
  toCard(e){
    let {eid,name,headimg,wxaccount,prodocutname,openid} =e.currentTarget.dataset
    let param = {
      name,headImg:headimg,wxAccount:wxaccount,prodocutName:prodocutname,openid
    }
    let { id } = e.currentTarget.dataset
    wx.navigateTo({url:`/pages/mycard/card?eId=${id}&param=${JSON.stringify(param)}&source=2`})
  },
  onLoad: function (options) {
    console.log(options)
    if(options.pageType){
      this.setData({pageType:options.pageType})
    }
    const that = this;
    wx.setNavigationBarTitle({
      title: '顾问列表'
    })
    if(!options.pageType || options.pageType=='buildingDetail'){
      wx.request({
        //url:`http://10.73.158.97:8099/applet/mobile/visitcard/employee/queryByPhones`,
        url:`${cf.config.pageDomain}/applet/mobile/visitcard/employee/queryByPhones`,
        method:'POST',
        data:{ phones:options.phones },
        //data:{phones:'13536235013,18924831855,17377106282,15099998471'},
        header: {
          'content-type': 'application/json'
        },
        success:function(res){
          res.data.map(item=>{
            if(item.labels){
              item.labels = JSON.parse(item.labels).map(child=>child.lab)
            }
          })
          that.setData({
            consultantInfoList:res.data
          })
        }
      })
    }
   
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if(this.data.pageType!='buildingDetail'){
      const {pageNo} = this.data
      this.setData({pageNo:pageNo+1})
      let data = this.changeParams()
      data.pageNo = pageNo+1
      this.getconsultantInfoList(data)
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  }
})